import { Button, Tooltip } from 'antd';
import { Fragment } from 'react';
import { EditOutlined } from '@ant-design/icons';
import { AreaNumManageAPI } from '../common/type';
import pageStyles from '@/common/pageLayout.less';

/** 行政区域管理页面功能操作 */
const getManageFunctions = (
  /** 资源权限参数 */
  permissions: Record<string, any>,
  /** 数据操作参数 */
  operateFnc: AreaNumManageAPI.ITableActionType,
  /** 数据操作对象 */
  record?: Record<string, any>,
) => {
  /** 功能操作集 */
  let operationList: any = [];

  /** 编辑操作 */
  const EditFunc =
    permissions['/areaNumController/edit'] && record ? (
      <Fragment key={record?.id + 'preEditOutLined'}>
        <Button
          className='manageBtntTxtStyle'
          type={'link'}
          size={'small'}
          icon={<EditOutlined />}
          onClick={() => {
            operateFnc?.onEdit(record);
          }}
        >
          编辑
        </Button>
      </Fragment>
    ) : (
      <></>
    );
  operationList = [EditFunc];
  return operationList;
};

/** 行政区域数据表格信息 展示和操作 */
const getManageTableColumns = (
  /** 资源权限参数 */
  permissions: Record<string, any>,
  /** 数据操作参数 */
  operateFnc: AreaNumManageAPI.ITableActionType,
) => {
  return [
    {
      title: '省份编码',
      dataIndex: 'provinceCode',
      key: 'provinceCode',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => a.provinceCode - b.provinceCode,
    },
    {
      title: '省份名称',
      dataIndex: 'provinceName',
      key: 'provinceName',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => (a.provinceName > b.provinceName ? 1 : -1),
    },
    {
      title: '市区编码',
      dataIndex: 'cityCode',
      key: 'cityCode',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => a.cityCode - b.cityCode,
    },
    {
      title: '市区名称',
      dataIndex: 'cityName',
      key: 'cityName',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => (a.cityName > b.cityName ? 1 : -1),
    },
    {
      title: '区县编码',
      dataIndex: 'countyCode',
      key: 'countyCode',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => a.countyCode - b.countyCode,
    },
    {
      title: '区县名称',
      dataIndex: 'countyName',
      key: 'countyName',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => (a.countyName > b.countyName ? 1 : -1),
    },
    {
      title: '修改时间',
      dataIndex: 'modifyTime',
      key: 'modifyTime',
      width: 150,
      visible: true,
      sorter: (a: any, b: any) => (a.modifyTime > b.modifyTime ? 1 : -1),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div
              className={pageStyles['over-flow-hidden']}
              style={{ maxWidth: 180 }}
            >
              {text || '--'}
            </div>
          </Tooltip>
        );
      },
    },
    {
      title: '修改人',
      dataIndex: 'modifyerName',
      key: 'modifyerName',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => (a.modifyerName > b.modifyerName ? 1 : -1),
      render: (text: string | null) => {
        return <span>{text ? text : '--'}</span>;
      },
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 90,
      visible: true,
      unDrag: true,
      fixed: 'right',
      render: (_text: any, record: any) => {
        return (
          <div
            style={{
              display: 'flex',
            }}
          >
            {getManageFunctions(permissions, operateFnc, record)}
          </div>
        );
      },
    },
  ];
};

export { getManageTableColumns };
